﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using Kingdee.BOS.Workflow.App.Core.PlugIn;
using Kingdee.BOS.Workflow.Models.Chart;
using Kingdee.BOS.Workflow.Models.EnumStatus;
using Kingdee.BOS.Workflow.ServiceHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace RK.DzWorkflow
{
    [Description("金蝶工作流扩展插件")]
    [HotUpdate]
    public class DzWorkflowExt : AbstractWorkflowExtPlugIn
    {
        public override void OnAssignUpdate(AssignUpdateEventArgs e)
        {
            base.OnAssignUpdate(e);
            var astatus = e.AssignStatus;
            var assigns = e.AssignIds;
            if (e.AssignStatus == AssignStatus.Assigned)
            {
                foreach( var assignId in assigns )
                {

                }
            }
            else if (e.AssignStatus == AssignStatus.Completed)
            {
                foreach (var assignId in assigns)
                {
                    DynamicObjectCollection doc = GetWorkflow(assignId);
                    if (doc.Count > 0)
                    {
                        foreach (var item in doc)
                        {
                            //流程实例
                            string procInstId = Convert.ToString(item["FPROCINSTID"]);
                            var proObj = WorkflowChartServiceHelper.GetWorkflowChartByProcInstId(this.Context, procInstId);
                            //获取所有节点
                            List<ChartActivityInfo> activityInfos = WorkflowChartServiceHelper.GetProcessRouter(this.Context, procInstId);
                            //foreach (ChartActivityInfo activityInfo in activityInfos)
                            //{
                            //    var title = activityInfo.ActionDetails.FirstOrDefault().Title;
                            //}
                        }
                    }
                }
            }           
        }

        private DynamicObjectCollection GetWorkflow(string assignId)
        {
            string sql = string.Format(@"/*dialect*/
            SELECT a.FASSIGNID,a.FPROCINSTID,b.FOBJECTTYPEID,b.FKEYVALUE 
            FROM T_WF_ASSIGN a
            LEFT JOIN T_WF_PIBIMAP b ON a.FPROCINSTID = b.FPROCINSTID
            --LEFT JOIN dbo.T_WF_APPROVALASSIGN c ON c.FASSIGNID = a.FASSIGNID
            --LEFT JOIN dbo.T_WF_APPROVALITEM d ON d.FAPPROVALASSIGNID = c.FAPPROVALASSIGNID
            --LEFT JOIN dbo.T_META_OBJECTTYPE_L e ON e.FID = b.FOBJECTTYPEID AND e.FLOCALEID = 2052
            --INNER JOIN dbo.T_WF_PROCINST f ON a.FPROCINSTID = f.FPROCINSTID
            --INNER JOIN dbo.T_WF_TEMPLATE g ON g.FTMPID = f.FTMPID
            where a.FASSIGNID = '{0}'
            ", assignId);
            DynamicObjectCollection doc = DBUtils.ExecuteDynamicObject(this.Context, sql);
            return doc;
        }
    }
}
